package com.zhx.threadcoreknowledge.stopthread;

/**
 * @author ：zhanghaixuan
 * @date ：Created in 2019-09-29 09:17
 * 用途         ： 每次循环中去阻塞线程中如何正确停止,如果有sleep或者 wait 就不需要每次迭代的时候  检查中断
 */
public class RigthWayStopThreadWithSleepEveryLoop {
    public static void main(String[] args) throws InterruptedException {
        Runnable runnable = () ->{
            try {
                int num = 0;

                while (num <= 10000
//                        && !Thread.currentThread().isInterrupted()
                ){
                    if(num % 100 == 0){
                        System.out.println(num +"可以被100整除");
                    }
                    num++;
                    Thread.sleep(10);
                }
                System.out.println("结束");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        };

        Thread thread = new Thread(runnable);
        thread.start();
        Thread.sleep(5000);
        thread.interrupt();
    }
}
